package com.mapull.date;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Locale;

/**
 * @author mapull
 */
public class MapullDate {

    public static void main(String[] args) throws ParseException {
//        dateFormat();
//        simpleFormat();
        datetimeFormat();
    }

    private static void dateFormat(){
        DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
        DateFormat df2 = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINA);
        DateFormat df3 = DateFormat.getDateInstance(DateFormat.LONG, Locale.CHINESE);

        String date1 = df1.format(new Date());
        String date2 = df2.format(new Date());
        String date3 = df3.format(new Date());

        System.out.println("date1 " + date1);
        System.out.println("date2 " + date2);
        System.out.println("date3 " + date3);
    }

    private static void simpleFormat() throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 日期转字符串
        String date1 = df.format(new Date());
        System.out.println(date1);

        // 字符串转日期
        Date parse = df.parse(date1);
        System.out.println(parse);
    }

    private static void datetimeFormat(){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse("2022-03-26 17:27:32", formatter);
        System.out.println(parse);
    }
}
